import java.util.Date;

import javax.persistence.EntityManager;

import training.bms.business.Blog;
import training.bms.business.Post;
import training.bms.business.Tag;
import training.bms.persistence.ManagerStrategy;
import training.bms.persistence.ManagerUtil;


public class ManytoManyTest {
	public static void main(String[] args) {
		new ManagerUtil().execute(
				new ManagerStrategy() {
					
					@Override
					public void execute(EntityManager manager) {
						Blog blog = new Blog();
						blog.setName("Blog");
						blog.setCreationDate(new Date());
						
						Post post = new Post();
						post.setTitle("Post");
						post.setContent("Blah blah blah");
						post.setCreationDate(new Date());
						
						post.setBlog(blog);
						
						manager.persist(blog);
						manager.persist(post);	
						
						Tag tag = new Tag();
						tag.setName("Tag");
						manager.persist(tag);
						
						tag.getPosts().add(post);
						manager.merge(tag);
						
						post.getTags().add(tag);
						manager.merge(post);
						
						
					}
				}
		);
		
	}
}
